home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
076-100
/
disk_081
/
icon
/
queens.icn
< prev
next >
Wrap
Text File
|
1992-05-06
|
1KB
|
56 lines
# QUEENS(6)
#
# Solutions to n nonattacking queens
#
# Stephen B. Wampler
#
# Last modified 8/14/84
#
global n, solution
procedure main(args)
local i
n := args[1] | 8 # 8 queens by default
if not(0 < integer(n)) then stop("parameter must be integer > 0")
solution := list(n) # ... and a list of column solutions
write(n,"-Queens:")
every q(1) # start by placing queen in first col.
end
# q(c) - place a queen in column c.
#
procedure q(c)
local r
static up, down, rows
initial {
up := list(2*n-1,0)
down := list(2*n-1,0)
rows := list(n,0)
}
every 0 = rows[r := 1 to n] = up[n+r-c] = down[r+c-1] &
rows[r] <- up[n+r-c] <- down[r+c-1] <- 1 do {
solution[c] := r # record placement.
if c = n then show()
else q(c + 1) # try to place next queen.
}
end
# show the solution on a chess board.
#
procedure show()
static count, line, border
initial {
count := 0
line := repl("| ",n) || "|"
border := repl("----",n) || "-"
}
write("solution: ", count+:=1)
write(" ", border)
every line[4*(!solution - 1) + 3] <- "Q" do {
write(" ", line)
write(" ", border)
}
write(" ")
end